package edu.unindra.elearning.form;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import usu.widget.ButtonGlass;
import usu.widget.Form;
import usu.widget.GlassPane;
import edu.unindra.elearning.db.ResourceDatabase;
import edu.unindra.elearning.form.addin.ELearningFormMain;
import edu.unindra.elearning.form.addin.ELearningFormSubMainClass;
import edu.unindra.elearning.form.addin.ELearningFormSubMainMateri;
import edu.unindra.elearning.form.addin.ElearningFormSubMainMateriSoal;
import edu.unindra.elearning.form.addin.ElearningFormSubMainMateriSoalReport;
import edu.unindra.elearning.toolbar.ToolbarBlackGray;
import edu.unindra.elearning.util.About;
import edu.unindra.elearning.util.PanelBackGround;

import java.awt.AWTException;
import java.awt.CardLayout;
import java.awt.Toolkit;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import usu.widget.constan.Animation;

import java.awt.Dimension;
import java.io.IOException;

public class ELearningForm extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2471410853205547369L;

	private ExecutorService executor;
	private GlassPane glassPane;
	private PanelBackGround mainPanel;
	private JPanel mainSubPanel;

	private ELearningFormMain eLearningFormMain;
	private ELearningFormSubMainClass eLearningSubFormMainClass;
	private ELearningFormSubMainMateri eLearningSubFormMainMateri;
	private ElearningFormSubMainMateriSoal eLearningSubFormMainMateriSoal;
	private ElearningFormSubMainMateriSoalReport eLearningSubFormMainMateriSoalReport;
	private ToolbarBlackGray toolBar;
	private About about;

	private ResourceDatabase rd;
	private JLabel titleBody;
	private ButtonGlass btnHome;

	private String kls;
	private String mtr;

	/** Creates new form ELearningForm */
	public ELearningForm() {
		setResizable(false);
		executor = Executors.newCachedThreadPool();

		getContentPane().setEnabled(false);
		setAnimationShow(Animation.SHOW_FROM_BOTTOM);
		setAnimationHide(Animation.HIDE_TO_BOTTOM);
		setLocationRelativeTo(null);
		setIconImage(Toolkit
				.getDefaultToolkit()
				.getImage(
						ELearningForm.class
								.getResource("/edu/unindra/elearning/image/network.png")));
		setTitle("Simulasi Ujian SDIT Raudhatul Muttaqin Bekasi");
		setMinimumSize(new Dimension(750, 640));

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 800) / 2, (screenSize.height - 600) / 2,
				800, 600);

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		initComponents();

		getRootPane().getInputMap().put(
				KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE), "CLOSE");
		getRootPane().getActionMap().put("CLOSE", new AbstractAction() {

			private static final long serialVersionUID = -1;

			public void actionPerformed(ActionEvent e) {
				((GlassPane) getGlassPane()).hideComponent();
			}
		});
		initActions();
	}

	private void initComponents() {
		glassPane = new usu.widget.GlassPane();

		mainPanel = new PanelBackGround();
		mainSubPanel = new JPanel();
		eLearningFormMain = new ELearningFormMain();
		eLearningSubFormMainClass = new ELearningFormSubMainClass();
		eLearningSubFormMainMateri = new ELearningFormSubMainMateri();
		eLearningSubFormMainMateriSoal = new ElearningFormSubMainMateriSoal();
		eLearningSubFormMainMateriSoalReport = new ElearningFormSubMainMateriSoalReport();
		about = new About();
		about.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				aboutActionPerformed(evt);
			}
		});

		toolBar = new ToolbarBlackGray();
		toolBar.setRollover(true);
		toolBar.setToolTipText("Tool Bar");

		btnHome = new ButtonGlass();
		btnHome.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnHome.setForeground(new java.awt.Color(255, 255, 255));
		btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/edu/unindra/elearning/image/Home 2.png"))); // NOI18N
		btnHome.setMnemonic('M');
		btnHome.setText("Menu");
		btnHome.setToolTipText("Klik untuk kembali ke menu utama");
		btnHome.setFocusable(false);
		btnHome.setRoundRect(true);
		btnHome.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		btnHome.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonHomeActionPerformed(evt);
			}
		});
		toolBar.add(btnHome);

		titleBody = new JLabel();
		titleBody.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
		titleBody.setForeground(new java.awt.Color(255, 255, 255));
		titleBody.setText("Simulasi Ujian SDIT Raudit Muttaqin Bekasi");

		mainSubPanel.setOpaque(false);
		mainSubPanel.setLayout(new java.awt.CardLayout());
		mainSubPanel.add(eLearningFormMain, "HOME");
		mainSubPanel.add(eLearningSubFormMainClass, "CLASS");
		mainSubPanel.add(eLearningSubFormMainMateri, "MATERI");
		mainSubPanel.add(eLearningSubFormMainMateriSoal, "SOAL");
		mainSubPanel.add(eLearningSubFormMainMateriSoalReport, "REPORT");

		setGlassPane(glassPane);
		glassPane.setAlphaDisable(10);
		glassPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER,
				100, 100));

		javax.swing.GroupLayout panelUtamaLayout = new javax.swing.GroupLayout(
				mainPanel);
		mainPanel.setLayout(panelUtamaLayout);
		panelUtamaLayout
				.setHorizontalGroup(panelUtamaLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelUtamaLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																mainSubPanel,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																764,
																Short.MAX_VALUE)
														.addComponent(titleBody))
										.addContainerGap()));
		panelUtamaLayout
				.setVerticalGroup(panelUtamaLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(titleBody)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												mainSubPanel,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												451, Short.MAX_VALUE)
										.addContainerGap()));

		getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
		getContentPane().add(toolBar, java.awt.BorderLayout.PAGE_START);

	}

	private void initActions() {
		eLearningFormMain.getBtnPlay().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (eLearningSubFormMainClass.isVisible()) {
					return;
				}

				showPanel(eLearningSubFormMainClass, "CLASS");
				setTitleBody("Choose Class");
			}
		});

		eLearningFormMain.getBtnExit().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (eLearningSubFormMainClass.isVisible()) {
					return;
				}
				exit();
			}
		});

		eLearningFormMain.getBtnAbout().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				((GlassPane) getGlassPane()).showComponent(about);
			}
		});

		eLearningSubFormMainClass.getBtnClass1().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						kls = "1";
						chooseMateri();
					}

				});

		eLearningSubFormMainClass.getBtnClass2().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						kls = "2";
						chooseMateri();
					}

				});

		eLearningSubFormMainClass.getBtnClass3().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						kls = "3";
						chooseMateri();
					}

				});

		eLearningSubFormMainClass.getBtnClass4().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						kls = "4";
						chooseMateri();
					}

				});

		eLearningSubFormMainClass.getBtnClass5().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						kls = "5";
						chooseMateri();
					}

				});

		eLearningSubFormMainClass.getBtnClass6().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						kls = "6";
						chooseMateri();
					}

				});

		eLearningSubFormMainMateri.getBtnMtk().addActionListener(
				new ActionListener() {

					@SuppressWarnings("static-access")
					@Override
					public void actionPerformed(ActionEvent arg0) {
						mtr = "matematika";

						eLearningSubFormMainMateriSoal.setKls(kls);
						eLearningSubFormMainMateriSoal.setMtr(mtr);

						try {
							String path = rd.getInstance()
									.getPathSoal(kls, mtr);
							if (path.length() > 0) {
								eLearningSubFormMainMateriSoal
										.initializedata(path);

								eLearningSubFormMainMateriSoal.getBtnNext()
										.setEnabled(true);
								if (eLearningSubFormMainMateriSoal.isVisible()) {
									return;
								}
								showPanel(eLearningSubFormMainMateriSoal,
										"SOAL");
								setTitleBody("Soal" + mtr);

							} else {
								backToMenu();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});

		eLearningSubFormMainMateri.getBtnBhsIndo().addActionListener(
				new ActionListener() {

					@SuppressWarnings("static-access")
					@Override
					public void actionPerformed(ActionEvent arg0) {
						mtr = "bhsindonesia";

						eLearningSubFormMainMateriSoal.setKls(kls);
						eLearningSubFormMainMateriSoal.setMtr(mtr);

						try {
							String path = rd.getInstance()
									.getPathSoal(kls, mtr);
							if (path.length() > 0) {
								eLearningSubFormMainMateriSoal
										.initializedata(path);

								eLearningSubFormMainMateriSoal.getBtnNext()
										.setEnabled(true);
								if (eLearningSubFormMainMateriSoal.isVisible()) {
									return;
								}
								showPanel(eLearningSubFormMainMateriSoal,
										"SOAL");
								setTitleBody("Soal" + mtr);

							} else {
								backToMenu();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}

					}

				});

		eLearningSubFormMainMateri.getBtnBhsEnglish().addActionListener(
				new ActionListener() {

					@SuppressWarnings("static-access")
					@Override
					public void actionPerformed(ActionEvent arg0) {
						mtr = "bhsinggris";

						eLearningSubFormMainMateriSoal.setKls(kls);
						eLearningSubFormMainMateriSoal.setMtr(mtr);

						try {
							String path = rd.getInstance()
									.getPathSoal(kls, mtr);
							if (path.length() > 0) {
								eLearningSubFormMainMateriSoal
										.initializedata(path);

								eLearningSubFormMainMateriSoal.getBtnNext()
										.setEnabled(true);
								if (eLearningSubFormMainMateriSoal.isVisible()) {
									return;
								}
								showPanel(eLearningSubFormMainMateriSoal,
										"SOAL");
								setTitleBody("Soal" + mtr);

							} else {
								backToMenu();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}

					}

				});

		eLearningSubFormMainMateriSoal.getBtnFinish().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						eLearningSubFormMainMateriSoalReport
								.setMtr(eLearningSubFormMainMateriSoal.getMtr());
						eLearningSubFormMainMateriSoalReport
								.setKls(eLearningSubFormMainMateriSoal.getKls());
						eLearningSubFormMainMateriSoalReport
								.setScore(eLearningSubFormMainMateriSoal
										.getScore());
						eLearningSubFormMainMateriSoalReport
								.setCountSoal(eLearningSubFormMainMateriSoal
										.getCountSoal());
						eLearningSubFormMainMateriSoalReport
								.setCountTrue(eLearningSubFormMainMateriSoal
										.getCountTrue());

						// eLearningSubFormMainMateriSoalReport.initComponents();
						try {
							eLearningSubFormMainMateriSoalReport
									.initializedata();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						if (eLearningSubFormMainMateriSoalReport.isVisible()) {
							return;
						}

						showPanel(eLearningSubFormMainMateriSoalReport,
								"REPORT");
						setTitleBody("Report");

					}
				});
	}

	private void backToMenu() {
		showTransition(eLearningFormMain);
		setTitleBody(null);
		((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, "HOME");
	}

	private void chooseMateri() {
		if (eLearningSubFormMainMateri.isVisible()) {
			return;
		}

		showPanel(eLearningSubFormMainMateri, "MATERI");
		setTitleBody("Choose Materi");
	}

	private void aboutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_aboutActionPerformed
		((GlassPane) getGlassPane()).hideComponent();
	}

	private void buttonHomeActionPerformed(java.awt.event.ActionEvent evt) {
		if (eLearningFormMain.isVisible()) {
			return;
		}
		showTransition(eLearningFormMain);
		setTitleBody(null);
		((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, "HOME");

	}

	private void showTransition(JComponent com) {
		try {
			((GlassPane) getGlassPane()).startTransition(com);
		} catch (AWTException ex) {
		}
	}

	public void showPanel(JPanel panel, String card) {
		if (panel.isVisible()) {
			return;
		} else {
			showTransition(mainPanel);
			((CardLayout) mainSubPanel.getLayout()).show(mainSubPanel, card);
		}
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosing
		exit();
	}

	@Override
	public void exit() {
		if (showConfirm("Anda Yakin?") != JOptionPane.OK_OPTION) {
			return;
		}
		executor.shutdown();
		super.exit();
	}

	private int showConfirm(String message) {
		return JOptionPane.showConfirmDialog(this, message, "Konfirmasi",
				JOptionPane.OK_CANCEL_OPTION);
	}

	public void setTitleBody(String title) {
		if (title == null) {
			titleBody.setText("e'Learning");
		} else {
			titleBody.setText("e'Learning - " + title);
		}
	}
}
